package com.hengxinguotong.zhihuichengjian.treeview;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.StructureRes;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.ui.engineering.task.ProjectStructureActivity;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by HQOCSHheqing on 2016/8/3.
 *
 * @description 适配器类，就是listview最常见的适配器写法
 */
public class NodeTreeAdapter extends BaseAdapter{

    //大家经常用ArrayList，但是这里为什么要使用LinkedList
    // ，后面大家会发现因为这个list会随着用户展开、收缩某一项而频繁的进行增加、删除元素操作，
    // 因为ArrayList是数组实现的，频繁的增删性能低下，而LinkedList是链表实现的，对于频繁的增删
    //操作性能要比ArrayList好。
    private LinkedList<Node> nodeLinkedList;
    private LayoutInflater inflater;
    private int retract;//缩进值
    private Context context;
    private ListView listView;

    public NodeTreeAdapter(Context context,ListView listView,LinkedList<Node> linkedList){
        inflater = LayoutInflater.from(context);
        this.context = context;
        nodeLinkedList = linkedList;
        this.listView = listView;
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                expandOrCollapse(position);
            }
        });
        //缩进值，大家可以将它配置在资源文件中，从而实现适配
        retract = (int)(context.getResources().getDisplayMetrics().density*10+0.5f);
    }

    /**
     * 展开或收缩用户点击的条目
     * @param position
     */
    private void expandOrCollapse(int position){
        Node node = nodeLinkedList.get(position);
        if (node != null && !(node.getIsLeaf() == 1)){
            boolean old = node.isExpand();
            if (old){
                List<Node> nodeList = node.get_childrenList();
                int size = nodeList.size();
                Node tmp = null;
                for (int i = 0;i < size;i++){
                    tmp = nodeList.get(i);
                    if (tmp.isExpand()){
                        collapse(tmp,position+1);
                    }
                    nodeLinkedList.remove(position+1);
                }
            }else{
                nodeLinkedList.addAll(position + 1, node.get_childrenList());
            }
            node.setIsExpand(!old);
            notifyDataSetChanged();
        }
    }
    /**
     * 递归收缩用户点击的条目
     * 因为此中实现思路是：当用户展开某一条时，就将该条对应的所有子节点加入到nodeLinkedList
     * ，同时控制缩进，当用户收缩某一条时，就将该条所对应的子节点全部删除，而当用户跨级缩进时
     * ，就需要递归缩进其所有的孩子节点，这样才能保持整个nodeLinkedList的正确性，同时这种实
     * 现方式避免了每次对所有数据进行处理然后插入到一个list，最后显示出来，当数据量一大，就会卡顿，
     * 所以这种只改变局部数据的方式性能大大提高。
     * @param position
     */
    private void collapse(Node node,int position){
        node.setIsExpand(false);
        List<Node> nodes = node.get_childrenList();
        int size = nodes.size();
        Node tmp = null;
        for (int i = 0;i < size;i++){
            tmp = nodes.get(i);
            if (tmp.isExpand()){
                collapse(tmp,position+1);
            }
            nodeLinkedList.remove(position+1);
        }
    }

    @Override
    public int getCount() {
        return nodeLinkedList.size();
    }

    @Override
    public Object getItem(int position) {
        return nodeLinkedList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null){
            convertView = inflater.inflate(R.layout.tree_listview_item,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        Node node = nodeLinkedList.get(position);
        holder.label.setText(node.get_label());
        if(node.getIsLeaf() == 1){	//0为父节点，1为子节点
            holder.rightIcon.setVisibility(View.INVISIBLE);
            if(node.get_level() == 1){
                holder.imageView.setImageResource(R.mipmap.first);
                holder.label.setTextColor(context.getResources().getColor(R.color.TextColorBlack));
                holder.label.setTextSize(16);
            }else{
                holder.imageView.setImageResource(R.mipmap.third);
                holder.label.setTextColor(context.getResources().getColor(R.color.color_adadad));
                holder.label.setTextSize(14);
            }
        }else{
            getStructureInfo(node);
            holder.rightIcon.setVisibility(View.VISIBLE);
            if(node.get_level() == 1){
                holder.imageView.setImageResource(R.mipmap.first);
                holder.label.setTextColor(context.getResources().getColor(R.color.TextColorBlack));
                holder.label.setTextSize(16);
            }else{
                holder.imageView.setImageResource(R.mipmap.second);
                holder.label.setTextColor(context.getResources().getColor(R.color.TextColorGray));
                holder.label.setTextSize(15);
            }
            if (node.isExpand()){
                holder.rightIcon.animate().rotation(90);
            }else{
                holder.rightIcon.animate().rotation(0);
            }
        }
//        holder.label.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(context,"选中:"+v.getTag(),Toast.LENGTH_SHORT).show();
//            }
//        });
        convertView.setPadding(node.get_level()*retract,15,15,15);
        return convertView;
    }

    static class ViewHolder{
        @BindView(R.id.id_treenode_icon)
        ImageView imageView;
        @BindView(R.id.right_icon)
        ImageView rightIcon;
        @BindView(R.id.id_treenode_label)
        TextView label;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
    private void getStructureInfo(final Node node) {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("constructionId", SPUtil.getString(context,"constructionid"));
            jsonObject.put("id", node.get_id());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData((BaseActivity) context, "/projectStructure/loadTree/" + SPUtil.getString(context,"uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {

                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    StructureRes structureRes = gson.fromJson(json, StructureRes.class);
                    if(structureRes.getStatus() == 1){
                        node.set_childrenList(structureRes.getValue());
                    }else{
                        ((BaseActivity)context).showToast(structureRes.getMsg());
                    }
                }
            }

            @Override
            public void onFailure(String json) {
            }
        });
    }

}
